// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Server side
// -----------------------------------------------
if (!isServer) exitWith {};

diag_log "WARCONTEXT: INITIALIZING MISSION";

// Init global variables
wcgarbage = [] call WC_fnc_serverinitconfig;

// UPSMON init
call compile preprocessFileLineNumbers "extern\Init_UPSMON.sqf";

// Init Debugger
wcgarbage = [] spawn WC_fnc_debug;

// Grab all WC_fnc_publicvariable events
if (isDedicated) then {
	wcgarbage = [] spawn WC_fnc_eventhandler;
};

wcgarbage = [] spawn WC_fnc_serverhandler;

//A3 TODO: setVehicleInit
// Add halo jump option at flag
if (wcwithhalojump == 1) then {
	flagusa setVehicleInit 'this addAction ["Halo Jump", "warcontext\actions\WC_fnc_dohalojump.sqf", [], -1, false]';
};

// Init Weather
if (wcwithweather == 1) then {
	wcgarbage = [wcrainrate] spawn WC_fnc_weather;
};

if (wcairopposingforce > 0) then {
	wcgarbage = [] spawn {
		waitUntil {format ["%1", wcselectedzone] != "[0,0,0]"};
		wcgarbage = [wcselectedzone, wcairpatroltype, wcairopposingforce] spawn WC_fnc_initairpatrol;
	};
};

if (wcwithseapatrol == 1) then {
	wcgarbage = [wcseainitpos] spawn WC_fnc_createseapatrol;
};

//A3 TODO: setVehicleInit
if (wcwithteleporttent == 1) then {
	flagusa setVehicleInit 'this addAction ["Teleport to TENT", "warcontext\actions\WC_fnc_doteleporttotent.sqf", [], -1, false]'; 
};

if (wcwithmhq == 1) then {
	flagusa setVehicleInit 'this addAction ["Teleport to MHQ", "warcontext\actions\WC_fnc_doteleporttomhq.sqf", [], -1, false]';
};
processInitCommands;

// Create mortuary
wcgarbage = [(getMarkerPos "mortuary")] spawn WC_fnc_createmortuary;

//A3 TODO: Vehicles respawn
{
	wcgarbage = [_x, 120] spawn WC_fnc_respawnvehicle;
	sleep 0.01;
} foreach wcrespawnablevehicles;

{
	if (wcwithcivilian > 0) then {
		wcgarbage = [_x] spawn WC_fnc_popcivilian;
	};

	if (wcwithsheeps == 1) then {
		if (random 1 > 0.9) then {
			wcgarbage = [position _x] spawn WC_fnc_createsheep;
		};
	};
} foreach wctownlocations;

if (wcwithcivilian > 0) then {
	wcgarbage = [] spawn WC_fnc_civilianinit;
};

//A3 TODO: Statics; Remove?
wcgarbage = [] spawn {
	private ["_bunker","_dir","_pos","_unit"];

	_bunker = nearestObjects [wcmapcenter, ["Land_fortified_nest_small_EP1"], 20000];
	{
		if (random 1 < wcstaticinbunkerprobability) then {
			_dir = getDir _x;
			_pos = getPos _x; 
			_unit = createVehicle ["DSHKM_TK_GUE_EP1", _pos, [], 0, "NONE"];
			_unit setPos _pos; 
			_unit setDir (_dir + 180);
		};
	} foreach _bunker;
};

// We must wait - async return bug of arma
sleep 1;

// Refresh global markers
wcgarbage = [120] spawn WC_fnc_refreshmarkers;

// Create random nuclear fire
if (wcwithnuclear == 1) then {
	wcgarbage = [] spawn {
		while {true} do {
			sleep (3800 + random 3800);
			if (random 1 > wcnuclearprobability) then {
				wcgarbage = [imam, 1] spawn WC_fnc_createnuclearfire;
			};
		};
	};
};

// Heartbeat of teamscore and detection
wcgarbage = [] spawn {
	private ["_lastteamscore","_lastalert"];
	_lastteamscore = 0;
	_lastalert = 0;
	while {true} do {
		if (wcalert > 100) then {wcalert = 100};
		if (wcfame < 0) then {wcfame = 0};		
		if (wcteamscore != _lastteamscore) then {
			["wcteamscore", "client"] call WC_fnc_publicvariable;
			_lastteamscore = wcteamscore;
		};
		if (wcalert != _lastalert) then {
			["wcalert", "client"] call WC_fnc_publicvariable;
			_lastalert = wcalert;
		};	
		sleep 5;
	};
};

// Decrease alert level by time
wcgarbage = [] spawn {
	private["_decrease","_enemys","_lastalert"];
	_lastalert = 0;
	while {true} do {
		_decrease = ceil (random 10);
		if (wcalert > _decrease) then {
			_enemys = (getMarkerPos "rescuezone") nearEntities ["CAManBase", wcdistance];
			if ({side _x in wcside} count _enemys == 0) then {
				wcalert = wcalert - _decrease;
				if (wcalert < 0) then {wcalert = 0};
				if (_lastalert != wcalert) then {
					["wcalert", "client"] call WC_fnc_publicvariable;
					_lastalert = wcalert;
				};
			};
		};
		sleep 60;
	};
};

// Manage player add score
if (wckindofserver != 3) then {
	wcgarbage = [] spawn WC_fnc_playerscore;
};

/////////////////////////////////////
// Create radiation on nuclear zone
/////////////////////////////////////

wcgarbage = [] spawn WC_fnc_radiationzone;

///////////////////////////////////////////////////////
// For open game - insert JIP players in team members
///////////////////////////////////////////////////////

if (wckindofserver != 1) then {
	wcgarbage = [] spawn {
		private ["_array","_knownplayer","_player","_lastinteam"];

		// Array contains known player (diff jip & player)
		_knownplayer = [];

		while {true} do {
			_array = [];
			
			{
				_player = name _x;
				if !(_player in _knownplayer) then {
					_array = _array + [_player];
					_knownplayer = _knownplayer + [_player];
				};
				sleep 0.01;
			} foreach playableUnits;

			_lastinteam = wcinteam + _array;

			if (format ["%1", wcinteam] != format["%1", _lastinteam]) then {
				wcinteam = _lastinteam;
				["wcinteam", "client"] call WC_fnc_publicvariable;
			};
			sleep 60;
		};
	};
};

/////////////////////////////////
// Synchronize the players rank
/////////////////////////////////

wcgarbage = [] spawn {
	private ["_lastranksync"];
	_lastranksync = [];
	while {true} do {
		wcranksync = [];
		{
			wcranksync = wcranksync + [[_x, rank _x]];
			sleep 0.01;
		} foreach playableUnits;
		if (format ["%1", _lastranksync] != format ["%1", wcranksync]) then {
			_lastranksync = wcranksync;
			["wcranksync", "client"] call WC_fnc_publicvariable;
		};
		sleep 60;
	};
};

//A3 TODO: Use {} brackets?
onPlayerConnected "wcgarbage = [_id, _name] spawn WC_fnc_publishmission;";
onPlayerDisconnected "wcplayerready = wcplayerready - [_name];";

////////////////////////////////////
// INIT GAME - MAIN LOOP
////////////////////////////////////

wcgarbage = [] spawn WC_fnc_mainloop;